package com.coolingme.ticket;

import java.util.HashMap;
import java.util.Map;

/**
 * 工厂
 *
 * @author wangyue
 * @date 2020-09-16 23:02
 */
public class TickeFactory {

    /**
     * 缓存池
     */
    private static Map<String, ITicket> ticketPool = new HashMap<>();

    /**
     * 从缓存池中获取火车票
     *
     * @param from 从哪来
     * @param to   到哪去
     * @return 火车票
     */
    public static ITicket getTicket(String from, String to) {
        String key = from + "->" + to;
        if (!ticketPool.containsKey(key)) {
            System.out.println("首次创建，添加进缓存");
            ITicket ticket = new TrainTicket(from, to);
            ticketPool.put(key, ticket);
        }
        return ticketPool.get(key);
    }

}
